/*
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file uiproxy.h
 * @author Marco Buzzanca 
 *
 * This header file contains UIProxy class implementation.
 */

#include "uiproxy.h"

UIProxy::UIProxy()
  : _client() { }

UIProxy::UIProxy(const Address& UI_address)
  : _client(UI_address) { }

UIProxy::UIProxy(const std::string& UI_IP, boost::uint16_t UI_port)
  : _client(UI_IP, UI_port) { }

UIProxy::~UIProxy() { }

const Address& UIProxy::getUIAddress() const
{ return _client.getAddress(); }

void UIProxy::setUIAddress(const Address& UI_address)
{ _client.setAddress(UI_address); }

void UIProxy::setUIAddress(const std::string& UI_IP, boost::uint16_t UI_port)
{ _client.setAddress(UI_IP, UI_port); }

std::string UIProxy::getFile(const TaskDescriptor& td)
{
    return this->getFile(td.getLink());
}

std::string UIProxy::getFile(const std::string& link)
{
    using namespace boost::filesystem;

    path works("./works/");

    if (!exists(works))
        create_directory(works);

    _client.resolve();

    // Send link to the UI, so it knows what file we are asking for
    _client.send(link);

    std::stringstream ss;

    do
    {
        // Ǵenerates temporary filename
        ss.str("");
        ss << "./works/" << (unsigned int)(rand() + 100000 * rand());

    } while (exists(ss.str()));

    return ss.str();
}
